package cn.kw0501.tms.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.kw0501.tms.entity.Sms;
import cn.kw0501.tms.dao.SmsDao;
import cn.kw0501.tms.service.SmsService;
import cn.kw0501.tms.utils.JsonResult;
import cn.kw0501.tms.utils.SmsUtil;
import cn.kw0501.tms.utils.UuidUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


/**
 * <p>
 * 系统_短信 服务实现类
 * </p>
 *
 * @author Aedes
 * @since 2022-11-18
 */
@Service
public class SmsServiceImpl extends ServiceImpl<SmsDao, Sms> implements SmsService {

    @Autowired
    SmsDao smsDao;

    @Override
    public JsonResult sendSms(String phone) {
        String code = RandomUtil.randomNumbers(6);
        if(!SmsUtil.sendSms(phone,code,1)){
            return JsonResult.err(1022,"短信发送失败");
        }
        Sms sms = new Sms()
                .setId(UuidUtil.getUUID())
                .setCode(code)
                .setCreateTime(LocalDateTime.now())
                .setPhone(phone)
                .setState(0);

        smsDao.insert(sms);
        
        return JsonResult.ok("发送成功,请查询手机");
    }
}
